package com.newenergy.wisehome.data.ormlite.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.newenergy.wisehome.common.helper.LogHelper;
import com.newenergy.wisehome.data.bean.AirConditioning;
import com.newenergy.wisehome.data.ormlite.db.DatabaseHelper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * 空调数据库操作
 */

public class AirConditioningDao {
    private Context mContext;
    private Dao<AirConditioning, Integer> airConditioningDaoOpe;
    private DatabaseHelper helper;

    public AirConditioningDao(Context context) {
        mContext = context;
        try {
            helper = DatabaseHelper.getHelper(mContext);
            airConditioningDaoOpe = helper.getDao(AirConditioning.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加一个用户
     * @throws SQLException
     *
     */
    public void add(AirConditioning airConditioning) {
        try {
            airConditioning.setChecked(false);
            airConditioningDaoOpe.create(airConditioning);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    public int updateAirConditioning(AirConditioning airConditioning) {
        try {
            airConditioning.setChecked(false);
            return airConditioningDaoOpe.update(airConditioning);
            //    LogHelper.i("updateLightAirConditioningnumber" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public AirConditioning get(int id) {
        try {
            return airConditioningDaoOpe.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public AirConditioning getByUuid(String uuid) {
        List<AirConditioning> airConditioningList = null;
        try {
            airConditioningList = airConditioningDaoOpe.queryBuilder().where().eq
                    ("uuid", uuid).query();
            if (airConditioningList != null && airConditioningList.size() > 0) {
                return airConditioningList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @return
     */
    public List<AirConditioning> getAllAirConditioning() {
        List<AirConditioning> airConditioningList = new ArrayList<>();
        try {
            airConditioningList = airConditioningDaoOpe.queryBuilder().query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return airConditioningList;
    }

    public AirConditioning getById(String groupId,int category) {
        List<AirConditioning> airConditioningList = null;
        try {
            airConditioningList = airConditioningDaoOpe.queryBuilder().where().eq
                    ("groupId", groupId).and().eq
                    ("category", category).query();
            if (airConditioningList != null && airConditioningList.size() > 0) {
                return airConditioningList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public List<AirConditioning> getFamilyAirConditionings(String familyId,int category) {
        List<AirConditioning> airConditioningList = new ArrayList<>();
        try {
            airConditioningList = airConditioningDaoOpe.queryBuilder().where().eq ("familyId", familyId).and().eq
                    ("category", category).query();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return airConditioningList;
    }

    public int delete(AirConditioning airConditioning) {
        try {
            return airConditioningDaoOpe.delete(airConditioning);
            //    LogHelper.i("deleLightAirConditioningnumber==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public void deleteAll() {
        try {
            int number = airConditioningDaoOpe.deleteBuilder().delete();
            // LogHelper.i("deleteAll number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delete(List<AirConditioning> airConditioningList) {
        try {
            int number = airConditioningDaoOpe.delete(airConditioningList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public void deleteFamilyAirConditionings(String familyId) {
        try {
            List<AirConditioning> airConditioningList = getFamilyAirConditionings(familyId,8);
            int number = airConditioningDaoOpe.delete(airConditioningList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public AirConditioning getAirConditioningByName(String familyId, String name,int category) {
        try {
            List<AirConditioning> airConditioningList = airConditioningDaoOpe.queryBuilder().where().eq
                    ("familyId", familyId).and().eq
                    ("name", name).and().eq
                    ("category", category).query();
            if (airConditioningList != null && airConditioningList.size() > 0)
                return airConditioningList.get(0);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
